package cn.edu.cqu.newslab.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.util.Assert;


public class ShutDownHook implements Runnable,BeanFactoryAware {
	
	private static final Log logger = LogFactory.getLog(ShutDownHook.class);
	private ConfigurableListableBeanFactory beanFactory;
	@Override
	public void run() {
		if(this.beanFactory !=null){
			logger.info("&&[hmy]&&Destory singletons...");
			this.beanFactory.destroySingletons();
		}
	}
	
	@Override
	public void setBeanFactory(BeanFactory arg0) throws BeansException {
		if(arg0 instanceof DefaultListableBeanFactory){
			this.beanFactory = (ConfigurableListableBeanFactory)arg0;
			Runtime.getRuntime().addShutdownHook(new Thread(this));
		}
	}
}
